# -*- coding: utf-8 -*-
import pymysql

config = {
          'host':'120.77.39.182',
          'port':1899,
          'user':'ccihhlw',
          'password':'ccihhlw2017',
          'db':'ccih_hlwzx',
          'charset':'utf8',
          'cursorclass':pymysql.cursors.DictCursor,
          }

def GetNewids(_TableName):
    '''
        获取表主键ID
    '''
    objReturn = {"Value": "", "ReturnValue": "", "IsError": "", "ErrorCode": "", "ErrorMsg": ""}
    try:
        conn = pymysql.connect(**config)
        cur = conn.cursor()
        cur.nextset()
        cur.execute(" call GetNewids('" + _TableName + "') ")
        conn.commit() #提交,如果不加这句，存储过程会被模拟执行一次，有返回结果，但是在MySql中不会真正执行
        data = cur.fetchall()
        if data:
            objReturn["Value"]= str(data[0]['maxkey'])
            objReturn["ReturnValue"]=True
            objReturn["IsError"]=False
            objReturn["ErrorCode"] = 0
            objReturn["ErrorMsg"] = ""
        else:
            objReturn["Value"] = 0
            objReturn["ReturnValue"] = False
            objReturn["IsError"] = True
            objReturn["ErrorCode"] = 1
            objReturn["ErrorMsg"]="存储过程执行失败！未能获取到ID值！"
    except pymysql.Error, e:
        objReturn["Value"] = 0
        objReturn["ReturnValue"] = False
        objReturn["IsError"] = True
        objReturn["ErrorCode"]= e.args[0]
        objReturn["ErrorMsg"] = e.args[1]

    cur.close()
    conn.close()
    return objReturn


def Select(_request):
    '''
         执行查询语句返回列表
         '''
    objReturn = {"Value": "", "ReturnValue": "", "IsError": "", "ErrorCode": "", "ErrorMsg": "","RowNum":"0"}
    try:
        conn = pymysql.connect(**config)
        cur = conn.cursor()
        cur.execute(_request)
        data = cur.fetchall()
        if data:
            objReturn["Value"] = data
            objReturn["ReturnValue"] = True
            objReturn["IsError"] = False
            objReturn["ErrorCode"] = 0
            objReturn["ErrorMsg"] = ""
            objReturn["RowNum"]=len(data)
        else:
            objReturn["Value"] = ""
            objReturn["ReturnValue"] = False
            objReturn["IsError"] = True
            objReturn["ErrorCode"] = 1
            objReturn["ErrorMsg"] = "没能查询到匹配的值"
            objReturn["RowNum"] = 0
    except pymysql.Error,e:
        objReturn["Value"] = 0
        objReturn["ReturnValue"] = False
        objReturn["IsError"] = True
        objReturn["ErrorCode"] = e.args[0]
        objReturn["ErrorMsg"] = e.args[1]
        objReturn["RowNum"] = 0

    cur.close()
    conn.close()
    return objReturn

def Exec(_request):
    '''
    执行SQL指令返回True或False
    '''
    objReturn = {"Value": "", "ReturnValue": "", "IsError": "", "ErrorCode": "", "ErrorMsg": ""}
    try:
        conn = pymysql.connect(**config)
        cur = conn.cursor()
        doNum = cur.execute(_request)
        if doNum == 0:
            objReturn["Value"] = 0
            objReturn["ReturnValue"] = False
            objReturn["IsError"] = True
            objReturn["ErrorCode"] = 1
            objReturn["ErrorMsg"] = "执行失败！"
        else:
            objReturn["Value"] = doNum
            objReturn["ReturnValue"] = True
            objReturn["IsError"] = False
            objReturn["ErrorCode"] = 0
            objReturn["ErrorMsg"] = ""
    except pymysql.Error,e:
        objReturn["Value"] = 0
        objReturn["ReturnValue"] = False
        objReturn["IsError"] = True
        objReturn["ErrorCode"] = e.args[0]
        objReturn["ErrorMsg"] = e.args[1]
    cur.close()
    conn.close()
    return objReturn





